package icedot.library.common.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import icedot.library.common.R;
import icedot.library.common.base.BaseActivity;
import icedot.library.common.utils.StringUtils;

/**
 * Created by IceDot on 2016/3/12.
 */
public class WindowMessageDlg extends PopupWindow
{
    private BaseActivity _rootActivity;
    private View         _menuView;

    private View         _view_black;
    private View         _view_black2;
    private View         _view_black3;
    private View         _view_black4;
    private LinearLayout _layoutOk;
    private LinearLayout _layoutCancel;
    private LinearLayout _layoutSet;
    private TextView _btnOk;
    private TextView _btnCancel;
    private TextView _btnSet;
    private TextView _textContent;
    private TextView _textTitle;

    private String _content;
    private String _title;
    private String _okText;
    private String _setText;
    private String _cancelText;
    private boolean _canCancel = true;
    private int _cancelShow = View.VISIBLE;
    private int _okShow = View.VISIBLE;
    private int _setShow = View.GONE;
    private View.OnClickListener _cancelListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            dismiss();
        }
    };
    private View.OnClickListener _okListener;
    private View.OnClickListener _setListener;

    private View.OnClickListener _listenerClose = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(_canCancel)
            {
                dismiss();
            }
        }
    };

    public WindowMessageDlg(BaseActivity act)
    {
        _rootActivity = act;
        _content = "";
        _title = "";
        _okText = "";
        _cancelText = "";
        _setText = "";

        LayoutInflater inflater = (LayoutInflater) _rootActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        _menuView = inflater.inflate(R.layout.popwindow_message, null);
        //设置SelectPicPopupWindow的View
        this.setContentView(_menuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x0d000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

        initView(_menuView);
        initData();
    }

    private void initView(View v)
    {
        _view_black = v.findViewById(R.id.view_black);
        _view_black2 = v.findViewById(R.id.view_black2);
        _view_black3 = v.findViewById(R.id.view_black3);
        _view_black4 = v.findViewById(R.id.view_black4);

        _view_black.setOnClickListener(_listenerClose);
        _view_black2.setOnClickListener(_listenerClose);
        _view_black3.setOnClickListener(_listenerClose);
        _view_black4.setOnClickListener(_listenerClose);

        _textContent = (TextView)v.findViewById(R.id.dlgmessage_text_content);
        _textTitle = (TextView)v.findViewById(R.id.dlgmessage_text_title);
        _btnOk = (TextView)v.findViewById(R.id.dlgmessage_btn_ok);
        _btnCancel = (TextView)v.findViewById(R.id.dlgmessage_btn_cancel);
        _btnSet = (TextView)v.findViewById(R.id.dlgmessage_btn_set);
        _layoutCancel = (LinearLayout)v.findViewById(R.id.dlgmessage_layout_cancel);
        _layoutOk = (LinearLayout)v.findViewById(R.id.dlgmessage_layout_ok);
        _layoutSet = (LinearLayout)v.findViewById(R.id.dlgmessage_layout_set);
        _textContent.setText(_content);
        _textTitle.setText(_title);

        if(!StringUtils.isNullOrEmpty(_okText))
        {
            _btnOk.setText(_okText);
        }
        if(!StringUtils.isNullOrEmpty(_cancelText))
        {
            _btnCancel.setText(_cancelText);
        }
        if(!StringUtils.isNullOrEmpty(_setText))
        {
            _btnSet.setText(_setText);
        }

        if(_okListener == null)
        {
            _btnOk.setOnClickListener(_listenerClose);
        }
        else
        {
            _btnOk.setOnClickListener(_okListener);
        }

        if(_cancelListener == null)
        {
            _btnCancel.setOnClickListener(_listenerClose);
        }
        else
        {
            _btnCancel.setOnClickListener(_cancelListener);
        }

        if(_setListener == null)
        {
            _btnSet.setOnClickListener(_listenerClose);
        }
        else
        {
            _btnSet.setOnClickListener(_setListener);
        }

        _layoutCancel.setVisibility(_cancelShow);
        _layoutOk.setVisibility(_okShow);
        _layoutSet.setVisibility(_setShow);
        if(_okShow == View.VISIBLE)
        {
            _btnOk.requestFocus();
        }
        else if(_setShow == View.VISIBLE)
        {
            _btnSet.requestFocus();
        }
        else
        {
            _btnCancel.requestFocus();
        }

        _menuView.setOnKeyListener(new View.OnKeyListener()
        {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if (keyCode == KeyEvent.KEYCODE_BACK && !_canCancel)
                {
                    return true;
                }
                return false;
            }
        });
    }

    private void initData()
    {

    }

    public void setOkLinstener(View.OnClickListener linstener)
    {
        _okListener = linstener;
        if(_btnOk != null)
        {
            _btnOk.setOnClickListener(_okListener);
        }
    }

    public void setCancelLinstener(View.OnClickListener linstener)
    {
        _cancelListener = linstener;
        if(_btnCancel != null)
        {
            _btnCancel.setOnClickListener(_cancelListener);
        }
    }
    public void setSetLinstener(View.OnClickListener linstener)
    {
        _setListener = linstener;
        if(_btnSet != null)
        {
            _btnSet.setOnClickListener(_setListener);
        }
    }
    public void setContent(String content)
    {
        _content = content;
        if(_textContent != null)
        {
            _textContent.setText(_content);
        }
    }
    public String getContent()
    {
        return _content;
    }
    public void setTitle(String title)
    {
        _title = title;
        if(_textTitle != null)
        {
            _textTitle.setText(_title);
        }
    }
    public String getTitle()
    {
        return _title;
    }
    public TextView getOKButton()
    {
        return _btnOk;
    }
    public TextView getCancelButton()
    {
        return _btnCancel;
    }
    public TextView getSetButton()
    {
        return _btnSet;
    }
    public void setOkButtonText(String text)
    {
        _okText = text;
        if(_btnOk != null)
        {
            _btnOk.setText(_okText);
        }
    }
    public void setCancelButtonText(String text)
    {
        _cancelText = text;
        if(_btnCancel != null)
        {
            _btnCancel.setText(_cancelText);
        }
    }
    public void setSetButtonText(String text)
    {
        _setText = text;
        if(_btnSet != null)
        {
            _btnSet.setText(_setText);
        }
    }
    public void setOkBtnVisibility(int show)
    {
        _okShow = show;
        if(_layoutOk != null)
        {
            _layoutOk.setVisibility(show);
        }
    }
    public void setCancelBtnVisiable(int show)
    {
        _cancelShow = show;
        if(_layoutCancel != null)
        {
            _layoutCancel.setVisibility(show);
        }
    }
    public void setSetBtnVisiable(int show)
    {
        _setShow = show;
        if(_layoutSet != null)
        {
            _layoutSet.setVisibility(show);
        }
    }

    public void show(final View v)
    {
        v.post(new Runnable()
        {
            @Override
            public void run()
            {
                WindowMessageDlg.this.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });

    }

    public void setCancelable(boolean cancelable)
    {
        _canCancel = cancelable;
    }

    public static WindowMessageDlg showOneBtnDlg(View v,BaseActivity act, String title,String content)
    {
        if(act == null)
            return null;
        final WindowMessageDlg dlg = new WindowMessageDlg(act);
        dlg.setTitle(title);
        dlg.setContent(content);
        dlg.setCancelBtnVisiable(View.GONE);
        dlg.setSetBtnVisiable(View.GONE);
        dlg.setCancelable(false);
        dlg.setCancelLinstener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });
        dlg.show(v);
        return dlg;
    }
}
